package com.ymatou.iapi.productimporter.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productimporter.parameter.GetTaskListBean;
import com.ymatou.iapi.productimporter.service.GetTaskListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.ProductImportTask;
import com.ymttest.database.sqlwapper.ProductImporterWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
*分页获取导入任务列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetTaskList{
	private static GetTaskListBean gettasklistBean;
	private static GetTaskListCall gettasklistCall;
	private static int vipRootUserId = AgentUsers.vipRootUserId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分页获取导入任务列表");
	}

	@Before
	public void caseUp() {
		gettasklistBean=new GetTaskListBean();
		gettasklistCall=new GetTaskListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gettasklistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(GetTaskListBean gettasklistBean){
		ProductImporterWapper productImporterWapper = new ProductImporterWapper();
		int totalNum = productImporterWapper.selectTotalNumBySellerId(gettasklistBean.getSellerId());
		Logger.verifyEquals(gettasklistCall.getJsonObject().get("data").getAsJsonObject().get("total").getAsInt(),
				totalNum,"验证totalNum");
		gettasklistCall.getTaskInfos().forEach(x->{
			ProductImportTask productImportTask = productImporterWapper.selectProductImportTaskByTaskId(x.getTaskId());
			Logger.verifyEquals(x.getTaskStatus(),productImportTask.getTaskStatus(),"验证TaskStatus");
			Logger.verifyEquals(x.getFileName(),productImportTask.getLocalFilename(),"验证FileName");
			Logger.verifyEquals(x.getTotalNum(),productImportTask.getTotalNum(),"验证totalNum");
			Logger.verifyEquals(x.getSuccessNum(),productImportTask.getSuccessNum(),"验证successNum");
			Logger.verifyEquals(x.getFailNum(),productImportTask.getFailedNum(),"验证failNum");
			Logger.verifyEquals(x.getRemark(),productImportTask.getTaskStatusMsg(),"验证remark");
		});

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskList_01() {
		Logger.start(true,"分页获取导入任务列表");
		try {
			gettasklistBean.setSellerId(vipRootUserId);
			gettasklistBean.setAppId(TestDataManager.getRandomNum(6));
			gettasklistBean.setPageIndex(1);
			gettasklistBean.setPageSize(10);
			gettasklistCall.setData(gettasklistBean);
			gettasklistCall.callService();
			Logger.verifyEquals("true",gettasklistCall.getString("success"),"验证success");
			verifyDB(gettasklistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}